package exam.yingjiao.S3;

import util.ListUtil;

import java.util.*;

public class Main {

    public static int[] dx = {-1, 1, 0, 0};
    public static int[] dy = {0, 0, -1, 1};

    public static void main(String[] args) {
        String[][] matrix = new String[][] {
            {"abc",  "def",  "ghi"},
            {"aaa",  "aaa",  "aaa"},
            {"baa",  "bab",  "cde"}
        };
        ListUtil.printIntArray(new Main().getMinOperator(matrix));
    }

    public int[] getMinOperator(String[][] matrix) {
        int[] res = new int[matrix.length];
        char[][] graph = new char[3][3];
        for (int i=0; i<res.length; ++i) {
            for (int j = 0; j < 3; j++)
                graph[j] = matrix[i][j].toCharArray();
            res[i] = getMinOperator(graph);
        }

        return res;
    }

    private int getMinOperator(char[][] graph) {
        int[] cnt = new int[26];
        int maxCnt = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                cnt[graph[i][j] - 'a']++;
                maxCnt = Math.max(maxCnt, cnt[graph[i][j] - 'a']);
                if (maxCnt > 5)
                    return -1;
            }
        }
        if (judge(graph))
            return 0;
        int[] fuckCnt = new int[9];
        for (int i = 0; i < 26; i++) {
            if (cnt[i] > 0)
                fuckCnt[cnt[i]]++;
        }
        if (maxCnt == 2)
            return 1;
        if (maxCnt == 3)
            return 2;
        if (maxCnt == 4)
            return 3;
        return 4;
//        for (int i=0; i<3; ++i) {
//            for (int j = 0; j < 3; j++) {
//                int[] dir = findDir(graph, i, j);
//                if (dir == null)
//                    continue;
//                for (int d : dir) {
//                    int nx = i + dx[d], ny = j + dy[d];
//
//                }
//            }
//        }
    }

    private boolean judge(char[][] graph) {
        for (int i=0; i<3; ++i) {
            if (graph[i][0] == graph[i][1] || graph[i][1] == graph[i][2])
                return false;
        }
        for (int i=0; i<3; ++i) {
            if (graph[0][i] == graph[1][i] || graph[1][i] == graph[2][i])
                return false;
        }
        return true;
    }

    private void swap(char[][] graph, int x1, int y1, int x2, int y2) {
        char t = graph[x1][y1];
        graph[x1][y1] = graph[x2][y2];
        graph[x2][y2] = t;
    }

//    private int[] findDir(char[][] graph, int x, int y) {
//
//    }
}
